﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using DLL;

public partial class enQuesPage : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            BindData();
        }
        viewIsNoData();
    }

    //绑定数据源
    private void BindData()
    {
        gvExam.DataSource = examDesign_BLL.GetSource();
        gvExam.DataBind();
    }

    //绑定题型
    private void BindTX()
    {
        dropTX.DataSource = examDesign_BLL.GetTXSource(seleExamNum);
        dropTX.DataTextField = "txName";
        dropTX.DataValueField = "txNum";
        dropTX.DataBind();
    }

    /// <summary>
    /// 根据已有试卷编号获取试题
    /// </summary>
    private void BindQues()
    {
        if (seleExamNum == "")
            return;
        gvExistQues.DataSource = ques_BLL.GetQuesSource(seleExamNum);
        gvExistQues.DataBind();
    }

    #region 脚本
    /// <summary>
    /// 如果GridView中没有数据则显示提示信息
    /// </summary>
    protected void viewIsNoData()
    {
        if (gvExam.Rows.Count == 0)
        {
            RegisterStartupScript("prompt1", "<script type='text/javascript'>document.getElementById('divBrowse').innerHTML='(尚未找到相关数据)'</script>");
        }
        if (gvExistQues.Rows.Count == 0)
        {
            //RegisterStartupScript("prompt2", "<script type='text/javascript'>document.getElementById('divQues').innerHTML='(尚未找到相关数据)'</script>");
        }
    }
    /// <summary>
    /// 刷新当前页
    /// </summary>
    private void RefurPage()
    {
        RegisterStartupScript("refur", "<script type='text/javascript'>location.replace(location.href)</script>");
    }
    /// <summary>
    /// 显示消息
    /// </summary>
    /// <param name="msg">消息内容</param>
    protected void ShowMessage(string msg)
    {
        RegisterStartupScript("msg", "<script type='text/javascript' language='javascript'>alert('" + msg + "')</script>");
    }
    #endregion

    //选择行
    private static string seleExamNum = "";
    protected void gvExam_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
    {
        //获取当前选中试卷编号
        seleExamNum = gvExam.DataKeys[e.NewSelectedIndex][0].ToString();
        GridViewRow row = gvExam.Rows[e.NewSelectedIndex];
        labCureExam.Text = ((HyperLink)row.Cells[2].Controls[0]).Text;
        BindTX();
        BindQues();
    }

    //翻页
    protected void gvExam_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvExam.PageIndex = e.NewPageIndex;
        BindData();
    }
    //提交 
    protected void btnRefur_Click(object sender, EventArgs e)
    {
        if (txtTM.Text == "" || txtA.Text == "" || txtB.Text == "" || txtC.Text == "" || txtD.Text == "" || txtDA.Text == "")
        {
            ShowMessage("数据信息不完整！");
            return;
        }
        //创建新的试题库对象
        STK stk = new STK();
        stk.txNum = decimal.Parse(dropTX.SelectedValue);
        stk.stTM = txtTM.Text;
        stk.seleD = txtD.Text;
        stk.seleC = txtC.Text;
        stk.seleB = txtB.Text;
        stk.seleA = txtA.Text;
        stk.result = txtDA.Text;
        stk.analysis = txtFX.Text == "" ? "/" : txtFX.Text;
        //验证操作类型
        if (btnRefur.Text == "添加")
        {
            //执行添加
            bool bl = ques_BLL.AddQues(stk);
            if (bl)
            {
                ShowMessage("添加成功！");
                BindQues();
                btnCancel_Click(sender, e);
            }
            else
                ShowMessage("添加失败！");
        }
        else
        {
            //请求更新数据
            bool b = ques_BLL.UpdateQues(decimal.Parse(seleSTNum), stk);
            if (b)
            {
                ShowMessage("更新成功！");
                BindQues();
                btnCancel_Click(sender, e);
                btnRefur.Text = "添加";
            }
            else
                ShowMessage("更新失败！");

        }

    }

    //取消
    protected void btnCancel_Click(object sender, EventArgs e)
    {
        dropTX.ClearSelection();
        txtTM.Text = txtFX.Text = txtDA.Text = txtD.Text = txtC.Text = txtB.Text = txtA.Text = "";
        btnRefur.Text = "添加";
    }

    //选择试题
    private static string seleSTNum = "";
    protected void gvExistQues_SelectedIndexChanging1(object sender, GridViewSelectEventArgs e)
    {
        //获取当前试卷编号
        seleSTNum = gvExistQues.DataKeys[e.NewSelectedIndex][0].ToString();
        //获取当前选中行
        GridViewRow row = gvExistQues.Rows[e.NewSelectedIndex];
        //重置下拉列表选中项
        dropTX.ClearSelection();
        //为控件填充值 
        dropTX.Items.FindByText(((Label)row.FindControl("labTX")).Text).Selected = true;
        txtTM.Text = ((Label)row.FindControl("labTM")).Text;
        txtFX.Text = ((Label)row.FindControl("labFX")).Text;
        txtDA.Text = ((Label)row.FindControl("labDA")).Text;
        txtD.Text = ((Label)row.FindControl("labD")).Text;
        txtC.Text = ((Label)row.FindControl("labC")).Text;
        txtB.Text = ((Label)row.FindControl("labB")).Text;
        txtA.Text = ((Label)row.FindControl("labA")).Text;
        btnRefur.Text = "更新";
    }
    //翻页
    protected void gvExistQues_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvExistQues.PageIndex = e.NewPageIndex;
        BindQues();
    }

    //删除
    protected void gvExistQues_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //请求删除数据
        bool bl = ques_BLL.DeleteQues((decimal)gvExistQues.DataKeys[e.RowIndex][0]);
        if (bl)
        {
            ShowMessage("删除成功！");
            BindQues();
        }
        else
            ShowMessage("删除失败！");
    }
}
